package com.jmu.content.controller.user;


import com.jmu.constant.JwtClaimsConstant;
import com.jmu.content.dto.UserLoginDTO;
import com.jmu.content.pojo.User;
import com.jmu.content.properties.JwtProperties;
import com.jmu.content.result.Result;
import com.jmu.content.service.UserService;
import com.jmu.content.vo.UserLoginVO;
import com.jmu.context.BaseContext;
import com.jmu.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@RestController("userUserController")
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation("微信登录")
    public Result<UserLoginVO> userLogin(@RequestBody UserLoginDTO userLoginDTO){
        User user = userService.wxLogin(userLoginDTO);
        BaseContext.setCurrentId(1L);
        //生成jwt令牌
        Map<String,Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl()
                ,claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }

}

